// pages/index/index.js

var imageUtil = require('../../../utils/util.js');
const app = getApp()
var server = require("../../../utils/server.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagewidth: 0,//缩放后的宽
    imageheight: 0,//缩放后的高
    height: 0,
    detailheight: 0,
    rateheight: 0
  },

  imageLoad: function (e) {
    var imageSize = imageUtil.imageUtil(e)
    this.setData({
      imagewidth: imageSize.imageWidth,
      imageheight: imageSize.imageHeight
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    that.getCoupon();
    var query = wx.createSelectorQuery();
    // //选择id
    // var that = this;
    // query.select('.img:nth-child(1)').boundingClientRect(function (rect) {
    //   console.log(rect)
    //   return;
    //   console.log(rect.width)
    //   that.setData({
    //     height: rect.width
    //   })
    // }).exec();
  //   query.select('.detail').boundingClientRect(function (rect) {
  //     console.log(rect.height, rect.width)
  //     that.setData({
  //       detailheight: rect.width / 4
  //     })
  //   }).exec();
  //   query.select('.rate').boundingClientRect(function (rect) {
  //     console.log(rect.height, rect.width)
  //     that.setData({
  //       rateheight: rect.width
  //     })
  //   }).exec();
   },

  getCoupon:function(){
    var uid = app.globalData.userInfo.user_id;
    var that = this;
    server.postJSON("/User/getAllCouponList",{uid:uid},function(e){
      that.setData({
        list:e.data,
      })
    })
  },

  mycoupon:function(e){
    wx.navigateTo({
      url: '/pages/member/coupon/index',
    })
  },

  getcoupon:function(e){
    var uid = app.globalData.userInfo.user_id;
    if(uid == undefined){
      wx.showToast({
        title: '请先登录',
        icon:'none',
      })
      setTimeout(function(){
        wx.switchTab({
          url: '/pages/index/index',
        })
      },2000)
    }
    var id = e.currentTarget.dataset.id;
    server.postJSON("/User/get_coupon",{uid:uid,coupon_id:id},function(e){
      if(e.data.status == 0){
        wx.showToast({
          title: e.data.msg,
          icon:'none'
        })
        return;
      }
      if(e.data.status == 1){
        wx.showToast({
          title: e.data.msg,
          icon:"none"
        })
        setTimeout(function(){
          wx.navigateBack({
            delta:1,
          })
        },2000)
      }
    })
  },

  //事件处理函数
  bindViewTap: function () {  },

  
})